// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  var {
    op,
    _openid,
    gold
  } = event

  var myInfo = await db.collection("User").where({
    _openid: wxContext.OPENID
  }).get()
  myInfo = myInfo.data[0]

  if (_openid) {
    var userInfo = await db.collection("User").where({
      _openid: _openid,
    }).get()
    userInfo = userInfo.data[0]
  }
  //增加活跃值
  await db.collection('User').where({
    _openid: wxContext.OPENID,
  }).update({
    data: {
      active: _.inc(1),
    }
  })

  switch (op) {
    case 'robGold':
      // return gold
      return robGold(_openid);
      break;
    case 'robTreasure':
      return robTreasure(_openid);
      break;
    case 'hit':
      return hit(_openid);
      break;
    case 'feed':
      return feed(_openid);
      break;
    case 'giveGold':
      return giveGold(_openid);
      break;

    case 'feedMyPet':
      return feedMyPet();
      break;
    case 'train':
      return train();
      break;
   
  }



  //打劫金币
  async function robGold(_openid) {
    if (myInfo.pet.charm < 6) {
      return 'notEnough'
    }
    let gold = Math.floor((userInfo.gold) * (Math.random() * 5) / 1000)
    //减少
    await db.collection('User').where({
      _openid: _openid
    }).update({
      data: {
        gold: _.inc(-gold)
      }
    })
    //增加
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        gold: _.inc(gold)
      }
    })
    //增加记录
    await db.collection('Record').add({
      data: {
        _openid: wxContext.OPENID,
        createTime: db.serverDate(),
        content: {
          op: 'robGold',
          gold: gold,
          _openid: _openid
        }
      }
    })
    //减少自身魅力  
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        'pet.charm': _.inc(-6)
      }
    })

    return {
      gold: gold,
      charm: 6
    }

  }
  // 打劫宝物
  async function robTreasure(_openid) {
    if (myInfo.pet.charm < 6) {
      return 'notEnough'
    }
    let res = await db.collection('Treasure').where({
      _openid: _openid,
      amount: _.gt(0)
    }).get()
    let length = res.data.length

    let robArray = getRandomArrayElements(res.data, Math.floor(Math.random() * length))

    for (let i = 0; i < robArray.length; i++) {
      //减少
      await db.collection('Treasure').where({
        _id: robArray[i]._id
      }).update({
        data: {
          amount: _.inc(-1)
        }
      })
      //增加
      await db.collection('Treasure').where({
        _openid: wxContext.OPENID,
        type: robArray[i].type
      }).update({
        data: {
          amount: _.inc(1)
        }
      })
    }

    //增加记录
    await db.collection('Record').add({
      data: {
        _openid: wxContext.OPENID,
        createTime: db.serverDate(),
        content: {
          op: 'robTreasure',
          treasureAmount: robArray.length,
          _openid: _openid
        }
      }
    })

    //减少自身魅力
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
      
        'pet.charm': _.inc(-6)
      }
    })

    return {
      treasureAmount: robArray.length,
      charm: 6
    }

  }
  // 殴打精灵
  async function hit(_openid) {
    if (myInfo.pet.charm < 2) {
      return 'notEnough'
    }
    //减少
    await db.collection('User').where({
      _openid: _openid
    }).update({
      data: {
        'pet.life': _.inc(-2)
      }
    })

    //增加记录
    await db.collection('Record').add({
      data: {
        _openid: wxContext.OPENID,
        createTime: db.serverDate(),
        content: {
          op: 'hit',
          _openid: _openid
        }
      }
    })
    //减少自身魅力
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        'pet.charm': _.inc(-2)
      }
    })
    return {
      charm: 2
    }
  }
  // 喂养精灵
  async function feed(_openid) {
    if (userInfo.pet.life > 98) {
      return 'enough'
    }
    if (myInfo.gold < 10) {
      return 'goldNotEnough'
    }
    //增加
    await db.collection('User').where({
      _openid: _openid
    }).update({
      data: {
        'pet.life': _.inc(2)
      }
    })

    //增加记录
    await db.collection('Record').add({
      data: {
        _openid: wxContext.OPENID,
        createTime: db.serverDate(),
        content: {
          op: 'feed',
          _openid: _openid
        }
      }
    })
    //增加自身魅力
    let charm = myInfo.pet.charm > 98 ? 0 : 2
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        'gold': _.inc(-10),
        'pet.charm': _.inc(charm)
      }
    })
    return {
      charm: charm
    }
  }
  // 赠送金币
  async function giveGold(_openid) {
    if (myInfo.gold < gold) {
      return 'goldNotEnough'
    }
    //减少
    await db.collection('User').where({
      _openid: _openid
    }).update({
      data: {
        gold: _.inc(gold)
      }
    })
    //增加记录
    await db.collection('Record').add({
      data: {
        _openid: wxContext.OPENID,
        createTime: db.serverDate(),
        content: {
          op: 'giveGold',
          gold: gold,
          _openid: _openid
        }
      }
    })
    //增加自身魅力
    let charm = myInfo.pet.charm > 98 ? 0 : 2
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        'gold': _.inc(-gold),
        'pet.charm': _.inc(charm)
      }
    })
    return {
      charm: charm
    }
  }
  // 喂养自己的精灵
  async function feedMyPet() {
    if (myInfo.pet.life > 98) {
      return 'enough'
    }
    if (myInfo.gold < 10) {
      return 'goldNotEnough'
    }
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        gold: _.inc(-10),
        'pet.life': _.inc(2)
      }
    })
    return {
      life: 2
    }
  }
  // 训练自己的精灵
  async function train() {
    if (myInfo.pet.charm > 98) {
      return 'enough'
    }
    if (myInfo.gold < 6) {
      return 'goldNotEnough'
    }
    await db.collection('User').where({
      _openid: wxContext.OPENID
    }).update({
      data: {
        gold: _.inc(-6),
        'pet.charm': _.inc(2)
      }
    })
    return {
      charm: 2
    }
  }
  
  function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0),
      i = arr.length,
      min = i - count,
      temp, index;
    while (i-- > min) {
      index = Math.floor((i + 1) * Math.random());
      temp = shuffled[index];
      shuffled[index] = shuffled[i];
      shuffled[i] = temp;
    }
    return shuffled.slice(min);
  }

}